package com.mes.schedule.manager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.mes.basicdata.manager.CalendarDataManager;
import com.mes.schedule.domain.CombinedResource;
import com.mes.schedule.domain.Resource;
import com.mes.schedule.domain.SCalendar;
import com.mes.schedule.manager.iface.IScheduleResourceManager;


@Component("scheduleResourceManager")
@Transactional
public class ScheduleResourceManager implements IScheduleResourceManager{
	private final static Log log = LogFactory.getLog(ScheduleResourceManager.class);
	 
	@javax.annotation.Resource
	protected ScheduleCalendarManager scheduleCalendarManager;
	 @javax.annotation.Resource
	protected CalendarDataManager calendarDataManager;


	// private Map<String,ScheduleDeviceThread> deviceThreadMap =new
	// HashMap<String,ScheduleDeviceThread>();
	// private ThreadGroup deviceThreadGroup=new ThreadGroup("ScheduleDevice");
	//	

	public CalendarDataManager getCalendarDataManager() {
		return calendarDataManager;
	}

	public void setCalendarDataManager(CalendarDataManager calendarDataManager) {
		this.calendarDataManager = calendarDataManager;
	}
	/*
	 * 获取设备日历状态
	 */
	 
	public int getSegementCalendar(Resource resource, int segement) {
		
		
		// if(segement>128)
		// {
		// System.out.println("aaaaaa");}
		if (resource instanceof CombinedResource) {
			return this.getSegementCalendar((CombinedResource)resource, segement);
		}
		if(resource.getCalendar()==null)
		{
			log.error("发现设备日历为空!"+resource);
		}
		return this.scheduleCalendarManager.getSegementCalendar(resource.getCalendar(), segement);

	}
	
	
	/*
	 * 获取设备日历状态
	 */
	 
	public int getSegementCalendar(CombinedResource combinedResource, int segement) {
		List<Resource> subResourceList=combinedResource.getSubResourceList();
		int segementCalendar = SCalendar.CALENDAR_NOMAL;// 缺省为正常工作
		for (Resource resource : subResourceList) {
			// log.debug("资源信息 "+resource.getResUid()+"
			int subCalendar =this.getSegementCalendar(resource, segement);
			if (subCalendar == SCalendar.CALENDAR_CLOSINGTIME) {// 有一台设备不工作即不工作
				segementCalendar = SCalendar.CALENDAR_CLOSINGTIME;
				break;
			} else if (subCalendar == SCalendar.CALENDAR_OVERTIME) {// 有一台设备加班即加班
				segementCalendar = SCalendar.CALENDAR_OVERTIME;
			}
		}
		return segementCalendar;

	}


	public ScheduleCalendarManager getScheduleCalendarManager() {
		return scheduleCalendarManager;
	}


	public void setScheduleCalendarManager(
			ScheduleCalendarManager scheduleCalendarManager) {
		this.scheduleCalendarManager = scheduleCalendarManager;
	}



}
